//
//  SearchViewController.m
//  Alfresco
//
//  Created by Michael Muller on 10/5/09.
//  Copyright 2010 Zia Consulting.  Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
//

#import "SearchViewController.h"
#import "DocumentViewController.h"
#import "RepositoryItemTableViewCell.h"
#import "Utility.h"
#import "SearchResult.h"

@implementation SearchViewController

@synthesize search;
@synthesize table;
@synthesize results;
@synthesize searchDownload;
@synthesize progressBar;

- (void)dealloc {
	[search release];
	[table release];
	[results release];
	[searchDownload release];
	[progressBar release];
    [super dealloc];
}

- (void)viewDidLoad {
	self.title = @"Search Results";
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	RepositoryItemTableViewCell *cell = (RepositoryItemTableViewCell *) [tableView dequeueReusableCellWithIdentifier:RepositoryItemCellIdentifier];
    if (cell == nil) {
		NSArray *nibItems = [[NSBundle mainBundle] loadNibNamed:@"RepositoryItemTableViewCell" owner:self options:nil];
		cell = [nibItems objectAtIndex:0];
		NSAssert(nibItems, @"Failed to load object from NIB");
    }
    
	SearchResult *result = [self.results objectAtIndex:[indexPath row]];
	cell.filename.text = result.title;
	cell.details.text = [[NSString alloc] initWithFormat:@"relevance: %@", result.relevance];
	cell.image.image = imageForFilename(result.title);
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
	return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.results count];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
	
	SearchResultsDownload *down = [[SearchResultsDownload alloc] initWithSearchPattern:[searchBar text] delegate:self];
	self.searchDownload = down;
	[down start];
	[down release];
	[search resignFirstResponder];
}

- (void) asyncDownloadDidComplete:(AsynchonousDownload *)async {
	SearchResultsDownload *down = (SearchResultsDownload *) async;
	self.results = down.results;
	[table reloadData];
}

- (void) asyncDownload:(AsynchonousDownload *)async didFailWithError:(NSError *)error {
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
	[searchBar resignFirstResponder];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	SearchResult *result = [self.results objectAtIndex:[indexPath row]];
	self.progressBar = [DownloadProgressBar createAndStartWithURL:[NSURL URLWithString:result.contentLocation] delegate:self message:@"Downloading Document" filename:result.title];
}

- (void) download:(DownloadProgressBar *)down completeWithData:(NSData *)data {
	DocumentViewController *doc = [[DocumentViewController alloc] initWithNibName:@"DocumentViewController" bundle:[NSBundle mainBundle]];
	
	doc.fileData = data;
	doc.fileName = down.filename;
	
	doc.hidesBottomBarWhenPushed = YES;
	[self.navigationController pushViewController:doc animated:YES];
	
	[doc release];
}

- (void)viewWillAppear:(BOOL)animated {
	self.navigationController.navigationBarHidden = YES;
	[super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
	self.navigationController.navigationBarHidden = NO;
	[super viewWillDisappear:animated];
}


@end
